package com.swz.dcrm.ui.home;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Input;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.ApprovalViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ReasonFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.ll_defeated)
    LinearLayout llDefeated;
    @BindView(R.id.rg_defeated)
    RadioGroup rgDefeated;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_reason)
    EditText etReason;

    boolean hasBuy;

    long clueId;

    @Inject
    ApprovalViewModel approvalViewModel;
    @Inject
    MainViewModel mainViewModel;


    public static ReasonFragment newInstance() {

        return new ReasonFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_reason;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        ReasonFragmentArgs reasonFragmentArgs=ReasonFragmentArgs.fromBundle(getArguments());
        clueId=Long.valueOf(reasonFragmentArgs.getClueId());
        if(reasonFragmentArgs.getTitle().equals(getString(R.string.clue_detail_defeated))){
            llDefeated.setVisibility(View.VISIBLE);
        }else {
            llDefeated.setVisibility(View.GONE);
        }
        rgDefeated.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rb_yes:
                    hasBuy=true;
                    break;
                case R.id.rb_no:
                    hasBuy=false;
                    break;
            }
        });
        title.setText(reasonFragmentArgs.getTitle());
        tvTag.setText(reasonFragmentArgs.getTag());
        tvRight.setText(getString(R.string.confirm));
        mainViewModel.getInput().setValue(new Input(etReason,0,true));
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_right)
    public void confirm(){
        if(TextUtils.isEmpty(etReason.getText().toString())){
            showToast("请输入原因");
            return;
        }
            if(title.getText().toString().equals(getString(R.string.clue_detail_defeated))){
                approvalViewModel.applyDefeatedClue(clueId,hasBuy,etReason.getText().toString())
                        .observe(this, new Observer<BaseResponse<String>>() {
                            @Override
                            public void onChanged(BaseResponse<String> stringBaseResponse) {
                                showToast(stringBaseResponse.getMsg());
                                if(stringBaseResponse.isSuccess()){
                                    backClick();
                                }
                                approvalViewModel.getApplyDefeatedClue().removeObserver(this);
                            }
                        });
            }else if(title.getText().toString().equals(getString(R.string.clue_detail_hand_up))){
                approvalViewModel.applyHandUpClue(clueId,etReason.getText().toString())
                        .observe(this, new Observer<BaseResponse<String>>() {
                            @Override
                            public void onChanged(BaseResponse<String> stringBaseResponse) {
                                showToast(stringBaseResponse.getMsg());
                                if(stringBaseResponse.isSuccess()){
                                    backClick();
                                }
                                approvalViewModel.getApplyDefeatedClue().removeObserver(this);
                            }
                        });
            }else if (title.getText().toString().equals(getString(R.string.clue_detail_cancel_order))){
                approvalViewModel.applyCancelOrder(clueId,etReason.getText().toString())
                        .observe(this,stringBaseResponse -> {
                            if(stringBaseResponse!=null){
                                if(stringBaseResponse.isSuccess()){
                                    backClick();
                                }
                                showToast(stringBaseResponse.getMsg());
                            }
                        });
        }
    }
}
